#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sn_mempool.h"
#include "sn_rbtree.h"
#include "sn_config.h"

typedef struct my_test_rbtree_s{
	sn_rbtree_node_t  node;
	u_char            name[20];
}my_test_rbtree_t;

int main() {
	test_config();
}

int test_config() {
	sn_config_t         *conf;
	sn_mempool_t        *mpool;

	mpool = sn_create_mempool(32, 10);
	if (NULL == mpool) {
		printf("sn_create_mempool error\n");
		return -1;
	}

	conf = sn_create_config("/root/oschina/mod/conf/test.conf", mpool);
	if (NULL == conf) {
		printf("sn_create_config error\n");
		return -1;
	}

	printf("---------print-----------\n");
	sn_print_config(conf);

	sn_conf_node_values_t *val;
	val = sn_find_conf_node_value(conf, "network:node1");
	if (NULL == val) {
		printf("not find val\n");
		return -1;
	}
	int i;
	for (i=0; i<val->count; i++){
		printf("%s\n", val->arr[i]);
	}

	return 0;
}

int test_mpool() {
	int          i;
	void         *ptr;
	sn_mempool_t *mpool;

	mpool = sn_create_mempool(32, 10);
	if (NULL == mpool) {
		printf("sn_create_mempool error\n");
		return -1;
	}

	ptr = sn_mpalloc(mpool, 16);
#if 0
	for (i=0; i <10; i++) {
		ptr = sn_mpalloc(mpool, 512);
		if (NULL == ptr) {
			printf("sn_mpalloc error\n");
			return -1;
		}
		printf("memery add[%p]\n", ptr);
		sn_mpfree(ptr);
	}
#endif
	return 0;
}

int test_rbtree() {
	intptr_t         key;
	sn_rbtree_t      rbtree;
	sn_rbtree_node_t sentinel;
	my_test_rbtree_t tnode;
	my_test_rbtree_t *mnode;


	sn_rbtree_init(&rbtree, &sentinel, sn_rbtree_insert_value);

	memset(&tnode, 0x00, sizeof(my_test_rbtree_t));
	key = 123;

	sprintf(tnode.name, "hello");

	tnode.node.key = key;

	sn_rbtree_insert(&rbtree, (sn_rbtree_node_t *)&tnode);

	mnode = sn_rbtree_lookup(rbtree.root, &sentinel, key);
	if (NULL == mnode) {
		printf("sn_rbtree_lookup error\n");
		return -1;
	}

	printf("%s\n", mnode->name);

	return 0;
}
